<?php

Loader::using("ext::libs::date-formatter");

/**
 * Description of ActualizarDatos
 *
 * @author MFerreira
 * @export Modules
 * @export-metadata category:Datos:Actualizar mis datos
 * @export-metadata RequiredRoles:ROLE_STUDENT
 */
class ActualizarDatos extends Module {

    public function index() {
        /* @var $student Estudiantes */
        $student = $this->model->students->GetById($this->currentUser->getName());

        $this->assign("student", $student);
        $this->assign("estadosCiviles", Asociados::$EstadosCiviles);

        $this->assign("estratos", array("1", "2", "3", "4", "5", "6",));
        $this->assign("tiposDeSangre", TipoSangre::$nombresGrupos);
        $this->assign("RHs", array("+", "-"));
        
        $isEgresado = $this->request->get("NoFrame", 0);
        $this->assign("action", $this->route->action("Update", array("NoFrame" => $isEgresado,)));

        $this->assign("viewLaboral", $this->route->moduleAction("InformacionLaboral", "ShowInformacionLaboral"));
        $this->assign("viewAcademico", $this->route->moduleAction("InformacionAcademica", "ShowInformacionAcademica"));
        $this->assign("viewIdiomas", $this->route->moduleAction("InformacionIdiomas", "ShowInformacionIdiomas"));

        $this->assign("paises", $this->model->countries->GetAll(false));

        $this->display("index");
    }

    public function Update() {
        if ($this->request->submitted()) {
            /* @var $student Estudiantes */
            $student = $this->model->students->GetById($this->currentUser->getName());

            if (!$student->getTipoSangre()) {
                $student->setTipoSangre(new TipoSangre());
            }
            if (!$student->getLugarNacimiento()) {
                $student->setLugarNacimiento(new Municipios());
            }
            if (!$student->getConyugue()) {
                $student->setConyugue(new Personas());
            }

            $this->request->bind($student);

            $response = $this->model->students->Update($student);
            
            $this->updateChildren($student);

            $this->addMessage(new Message("Sus datos personales han sido actualizados.", "", Message::SUCCESS));
            $this->assign("action", $this->route->moduleAction("Perfiles", "index"));
            $this->display();
        } else {
            $this->index();
        }
    }

    private function updateChildren(Estudiantes &$student) {
        $children = $this->request->post("Hijos", array());
        $originalChildrenList = $student->getHijos();
        
        foreach ($originalChildrenList as /* @var $child Hijos */$child) {
            $this->model->children->delete($child->getHijoPk());
        }
        
        foreach ($children as $child) {
            $son = new Hijos();
            $son->getHijoPk()->setCodigoInterno($student->getCodigoInterno());
            $son->getHijoPk()->setNombre($child["Nombre"]);
            $son->getHijoPk()->setFechaNacimiento(dateToJson($child["FechaNacimiento"]));
            
            $this->model->children->add($son);
        }
    }

}

?>
